package LCR_code;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;

/**
 * @author blh
 * @date 2024年08月05日 18:48
 */
public class LCR_148_85 {
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        int len = putIn.length;
        // seen记录已经拿出的
        HashSet<Integer> seen = new HashSet<>();
        Deque<Integer> deque = new ArrayDeque<>();
        // 记录putIn的
        int sign = 0;
        for (int i = 0; i < takeOut.length; i++) {
            while (deque.isEmpty() || deque.peek() != takeOut[i]) {
                deque.push(putIn[sign]);
                sign++;
                if (sign >= len) {
                    return false;
                }
            }
            deque.pop();
        }
        return true;
    }
}
